package so.shanku.essential.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import aym.util.getservicesdata.GetServicesDataQueue;
import aym.util.getservicesdata.GetServicesDataUtil;
import aym.util.json.JsonMap;
import aym.util.json.JsonParseHelper;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseActivity;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.getdata.JsonKeys;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.StringUtil;
import so.shanku.essential.utils.Utils;
import so.shanku.essential.view.LayoutProductInfoParams2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jaelyn on 2015/9/19 0019.
 * 选择商品的参数
 */
public class SelectGoodsParamsActivity extends BaseActivity {
//	@ViewInject(id = R.id.expendlist)
//	private ExpandableListView goods_expandLv;

	@ViewInject(id = R.id.goods_price_tv)
	private TextView goods_price_tv;

	@ViewInject(id = R.id.select_goods_params_tv)
	private TextView select_goods_params_tv;


	@ViewInject(id = R.id.goods_params)
	private LinearLayout goods_params;


	@ViewInject(id = R.id.add_cart_tv, click = "add_cart_tv_click")
	private TextView add_cart_tv;

	private List<JsonMap<String, Object>> colorJsonMap;

	private String goodsId;
	private String goodsName;
	private String goodsPrice;
	/**
	 * 当前展示的商品的可选参数的列表信息
	 */
	private List<LayoutProductInfoParams2> pips;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_goods_params_layout);
		Bundle bundle = getIntent().getBundleExtra(Constant.BUNDLE_EXTRA);
		String colorJson = bundle.getString(Constant.COLOR_JSON_STR);
		colorJsonMap = JsonParseHelper.getList_JsonMap(colorJson);
		goodsId = bundle.getString(Constant.ID);
		goodsName = bundle.getString(Constant.NAME);
		goodsPrice = bundle.getString(Constant.GOODS_PRICE);
//		goodsSizeAdapter = new ExpandChildAdapter(this);
//		goodsColorAdapter = new ExpandChildAdapter(this);
		setParamsView();
		select_goods_params_tv.setText(getProductParamsSelectTitle());
		goods_price_tv.setText(StringUtil.getFormatMoneyWithSign(goodsPrice));
		int IsSelectStock =bundle.getInt(ExtraKeys.Key_Msg1);
		if(IsSelectStock>0) {
			add_cart_tv.setEnabled(true);
			add_cart_tv.setText(R.string.add2cart);
		} else {
			add_cart_tv.setEnabled(false);
			add_cart_tv.setText(R.string.salve_over);
		}

	}

	/**
	 * 设置显示选项
	 */
	private void setParamsView() {
		pips = new ArrayList<LayoutProductInfoParams2>();
		goods_params.removeAllViews();
		for (JsonMap<String, Object> jsonMap1 : colorJsonMap) {
			LayoutProductInfoParams2 params = new LayoutProductInfoParams2(this);
			params.setTitle(jsonMap1.getStringNoNull(JsonKeys.Key_ObjName));
			params.setParamsData(jsonMap1.getList_JsonMap("svLst"), 0);
			params.setItemClickListener(itemClickListener);
			goods_params.addView(params);
			pips.add(params);
		}
	}

	/**
	 * 当前所有的参数的参数改变事件
	 */
	private LayoutProductInfoParams2.MyOnItemClickListener itemClickListener = new LayoutProductInfoParams2.MyOnItemClickListener() {

		@Override
		public void itemClick(View tv, int position) {
			getService_getProudctInfoByByproidandSpec(getProductParamsSelect());
		}

	};

	/**
	 * 获取商品当前选中的 商品信息
	 */
	private String getProductParamsSelect() {
		String p = "";
		for (LayoutProductInfoParams2 pa : pips) {
			if (pa.getData() != null && pa.getTv_position() > -1
					&& pa.getTv_position() < pa.getData().size()) {
				int selectIndex = pa.getTv_position();
				JsonMap data_Select = pa.getData().get(selectIndex);
				p += data_Select.getStringNoNull("Id") + ",";
			}
		}
		if (p.length() > 0) {
			p = p.substring(0, p.length() - 1);
		}
		return p;
	}

	/**
	 * 获取商品当前选中的 商品信息
	 */
	private String getProductParamsSelectTitle() {
		String p = "";
		for (LayoutProductInfoParams2 pa : pips) {
			if (pa.getData() != null && pa.getTv_position() > -1
					&& pa.getTv_position() < pa.getData().size()) {
				int selectIndex = pa.getTv_position();
				JsonMap data_Select = pa.getData().get(selectIndex);
				p += data_Select.getStringNoNull(JsonKeys.Key_ObjName) + ",";
			}
		}
		if (p.length() > 0) {
			p = p.substring(0, p.length() - 1);
		}
		return p;
	}

	/**
	 * 获取商品规格的数据
	 */
	private void getService_getProudctInfoByByproidandSpec(String spec) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(GetDataQueue.Params_key,
				GetDataConfing.Key_proudctInfoByByproidandSpec);
		params.put("userName", Utils.getUserId(mContext));
		params.put("proId", goodsId);
		params.put("Spec", spec);
		GetDataQueue queue = new GetDataQueue();
		queue.setActionName(GetDataConfing.Action_proudctInfoByByproidandSpec);
		queue.setParamsNoJson(params);
		queue.setCallBack(callBack);
		queue.setWhat(GetDataConfing.What_proudctInfoByByproidandSpec);
		getDataUtil.getData(queue);
	}

	/**
	 * 选择颜色
	 *
	 * @param view
	 */
	public void goods_color_tv_click(View view) {
		if (colorJsonMap.size() == 0) {
			return;
		}

	}

	/**
	 * 选择尺码
	 *
	 * @param view
	 */
	public void goods_size_tv_click(View view) {
		if (colorJsonMap.size() == 0) {
			return;
		}
	}

	/**
	 * 加入购物车
	 *
	 * @param view
	 */
	public void add_cart_tv_click(View view) {
		add2Cart();
	}

	private void add2Cart() {
		loadingToast.show();
		loadingToast.update(R.string.add2cart_ing);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(GetDataQueue.Params_key, GetDataConfing.Key_AddShoppingCart);
		params.put("UserAccount", Utils.getUserAccount(this));
		params.put("proId", goodsId);
		params.put("proName", goodsName);

		params.put("speStr", getProductParamsSelect());//sizeId+","+colorId);
		params.put("Amount", 1);
		GetDataQueue queue = new GetDataQueue();
		queue.setActionName(GetDataConfing.GetAction_AddShoppingCart);
		queue.setParamsNoJson(params);
		queue.setCallBack(callBack);
		queue.setWhat(GetDataConfing.What_AddShoppingCart);
		getDataUtil.getData(queue);
	}

	private GetServicesDataUtil.IGetServicesDataCallBack callBack = new GetServicesDataUtil.IGetServicesDataCallBack() {

		@Override
		public void onLoaded(GetServicesDataQueue entity) {
			if (entity.isOk()) {
				if (ShowGetDataError.isCodeSuccess(mContext, entity.getInfo())) {
					if (entity.what == GetDataConfing.What_AddShoppingCart) {
						toast.showToast(R.string.add2cart_success);
						finish();
						MyApplication.getInstance().addShopCartNum();
					} else if (entity.what == GetDataConfing.What_proudctInfoByByproidandSpec) {
						JsonMap jsonMap = JsonParseHelper.getJsonMap_List_JsonMap(entity.getInfo(), JsonKeys.Key_Info).get(0);
						flushMsg(jsonMap);
					}
				}
			} else {
				ShowGetDataError.showNetError(mContext);
			}
			loadingToast.dismiss();
		}

	};

	/**
	 * 刷新提示信息
	 * @param info
	 */
	private void flushMsg(JsonMap info) {
		goodsPrice=info.getStringNoNull("ShowPrice");
		// &amp;lt;p&amp;gt
		// tv_proAfterService.setText(Html.fromHtml(info
		// .getStringNoNull("AfterService")
		// .replace("&amp;lt;p&amp;gt;", "")
		// .replace("&amp;lt;/p&amp;gt;", "")
		// s
		// .replace("&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;",
		// "")
		// e
		// ));
		boolean has_product = info.getInt("Stock") > 0;
		if (has_product) {
			add_cart_tv.setEnabled(true);
			add_cart_tv.setText(R.string.add2cart);
		} else {
			add_cart_tv.setEnabled(false);
			add_cart_tv.setText(R.string.salve_over);
			//ib_jiarugouwuche.setVisibility(View.INVISIBLE);
		}
		select_goods_params_tv.setText(getProductParamsSelectTitle());
		goods_price_tv.setText(StringUtil.getFormatMoneyWithSign(goodsPrice));
		//add_cart_tv.setEnabled(true);//test
	}
}